package com.example.videolib

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.baselib.BaseViewModel
import com.example.baselib.constants.MyConstants
import com.example.baselib.databean.UserData
import com.example.videolib.bean.RcvData
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * Author: sxygsj
 * Date: 2020/5/8 15:49
 * Description: Video
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class VideoViewModel(application: Application):BaseViewModel(application) {
    private val repository=VideoRepository()
    private val userData: UserData = MyConstants.userData
    private lateinit var rcvListData:MutableLiveData<List<RcvData>>
    private lateinit var videoUrl:MutableLiveData<String>
    private  var unitinfo: Map<String,String> = HashMap<String,String>()
    fun setUnitInfo(unitinfo: Map<String,String>){
        this.unitinfo=unitinfo
    }
    fun getUnitInfo():  Map<String,String> {
        return unitinfo
    }

    fun getUserData(): UserData {
        return userData
    }

    fun getRcvData():LiveData<List<RcvData>>{
        rcvListData= MutableLiveData<List<RcvData>>()
        return rcvListData
    }
    fun loadRcvData()=launchUI {
       val  result=repository.loadRcvData(userData.token,userData.unitinfo[0].unitcode)
        val type = object : TypeToken<List<RcvData>>() {}.type
       rcvListData.value= Gson().fromJson<List<RcvData>>(result.body()?.data, type)
    }

    fun getUrl(cameraIndexCode: String, streamType: Int) :LiveData<String>{
        isLoad=false
        videoUrl=MutableLiveData<String>().also { loadUrl(cameraIndexCode,streamType) }
        return videoUrl
    }

     fun loadUrl(cameraIndexCode: String, streamType: Int) =launchUI{
         val map=HashMap<String,String>()
         map["unitcode"]=userData.unitinfo[0].unitcode
         map["cameraIndexCode"]= cameraIndexCode
        val result=repository.loadUrl(userData.token,map,streamType)
        videoUrl.value= result.body()?.data
     }
}